package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.dao.TodoMapper;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Todo;
import com.itheima.service.TodoService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.UuidUtil;
import com.itheima.vo.TodoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class TodoServiceImpl implements TodoService {

    @Autowired
    private TodoMapper todoMapper;

    /**
     * 增加待办事项
     * @param todoVo
     */
    //TODO: 2020/9/1 adminUid没放进去
    @Override
    public void add(TodoVo todoVo) {
        Todo todo = new Todo();
        BeanUtils.copyProperties(todoVo,todo);
        todo.setUid(UuidUtil.getUuid());
        todo.setStatus((byte) 1);
        todo.setCreateTime(DateUtils.getNowDate());
        todo.setUpdateTime(DateUtils.getNowDate());
        todoMapper.insertSelective(todo);
    }

    /**
     * 删除待办事项
     * @param todoVo
     */
    @Override
    public void delete(TodoVo todoVo) {
        Todo todo = new Todo();
        BeanUtils.copyProperties(todoVo, todo);
        todo.setUid(todoVo.getUid());
        todo.setUpdateTime(DateUtils.getNowDate());
        todo.setStatus((byte) 0);
        todoMapper.updateByPrimaryKeySelective(todo);
    }

    /**
     * 编辑待办事项
     * @param todoVo
     */
    @Override
    public void edit(TodoVo todoVo) {
        Todo todo = new Todo();
        BeanUtils.copyProperties(todoVo, todo);
        todo.setUid(todoVo.getUid());
        todo.setUpdateTime(DateUtils.getNowDate());
        todoMapper.updateByPrimaryKeySelective(todo);
    }

    /**
     * 获取待办事项列表
     * @param keyword
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageResult getList(String keyword, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Map> mapList = todoMapper.findAllByKeyword(keyword);

        PageResult pageResult = new PageResult();
        pageResult.setRecords(mapList);
        pageResult.setTotal((long) mapList.size());
        pageResult.setCurrent(currentPage);
        pageResult.setSize(pageSize);

        return pageResult;
    }
}
